﻿using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using ManagerClient.AuthentificationService;
using ManagerClient.Manage.Views;
using Microsoft.Practices.Prism.Commands;

namespace ManagerClient.Manage.Logic
{
    public class ManageDepositsViewModel : TabPageViewModelBase
    {
        private IEnumerable<User> users;

        public ManageDepositsViewModel(AuthentificationServiceClient client)
        {
            Title = "Управление счетами пользователей";
            AddUserCommand = new DelegateCommand(AddUser);
            IsBusy = true;


            IObservable<IEnumerable<User>> func = Observable.FromAsyncPattern<int, IEnumerable<User>>(
                client.BeginGetAllUsersForDepartment, client.EndGetAllUsersForDepartment)(1);
            func.
                ObserveOnDispatcher().
                Finally(() => { IsBusy = false; }
                )
                .Subscribe(x => { Users = x; },
                           e => { }
                );
        }

        public IEnumerable<User> Users
        {
            get { return users; }
            set
            {
                users = value;
                RaisePropertyChanged(() => Users);
            }
        }

        public DelegateCommand AddUserCommand { get; private set; }

        private void AddUser()
        {
            var w = new UserWindow {DataContext = Container.Container.Instance.GetInstance<UserWindowViewModel>()};
            w.Show();
        }
    }
}